 package com.vtquang.mymaps;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.vtquang.mymaps.adapter.PlaceListAdapter;
import com.vtquang.mymaps.place.PlacesChose;
import com.vtquang.mymaps.place.atm;
import com.vtquang.mymaps.place.cafe;
import com.vtquang.mymaps.place.food;
import com.vtquang.mymaps.place.hospital;
import com.vtquang.mymaps.place.postoffice;
import com.vtquang.mymaps.place.university;

public class GetPlace_Activity extends Activity implements OnItemClickListener, OnSeekBarChangeListener {

	public static final String PLACE_CHOSE_KEY = "place_choose";
	public static final String DISTANCE_KEY = "distance";
	public static final int PLACE_CHOSE_ACTIVITY = 1;

	private PlaceListAdapter adapter;
	private ArrayList<PlacesChose> listPlace;
	private ListView lv;

	private Double lon;
	private Double lat;
	private TextView tv;
	private SeekBar seekbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_place);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		lon = bundle.getDouble(MainActivity.MYLOCATION_LON_KEY);
		lat = bundle.getDouble(MainActivity.MYLOCATION_LAT_KEY);

		seekbar = (SeekBar)findViewById(R.id.seekBar1);
		lv = (ListView) findViewById(R.id.lvPlace);
		tv = (TextView)findViewById(R.id.tvdistance);
		lv.setOnItemClickListener(this);

		listPlace = getListPlace();
		adapter = new PlaceListAdapter(this, listPlace);

		lv.setAdapter(adapter);
		
		seekbar.setOnSeekBarChangeListener(this);
		 
	}

	private ArrayList<PlacesChose> getListPlace() {
		ArrayList<PlacesChose> results = new ArrayList<PlacesChose>();
		results.add(new atm());
		results.add(new cafe());
		results.add(new food());
		results.add(new hospital());
		results.add(new postoffice());
		results.add(new university());

		return results;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		PlacesChose place = listPlace.get(position);

		Intent intent = new Intent(this, Place_Location_Activity.class);
		Bundle bundle = new Bundle();
		bundle.putCharSequence(PLACE_CHOSE_KEY, place.get_KeyWord());
		bundle.putDouble(MainActivity.MYLOCATION_LON_KEY, lon);
		bundle.putDouble(MainActivity.MYLOCATION_LAT_KEY, lat);
		bundle.putInt(this.DISTANCE_KEY, seekbar.getProgress() + 1000);
		intent.putExtras(bundle);
		startActivityForResult(intent, PLACE_CHOSE_ACTIVITY);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case PLACE_CHOSE_ACTIVITY:
			if (resultCode == RESULT_OK) {
				setResult(RESULT_OK, data);
				finish();
			} else {

			}
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		int value = seekBar.getProgress() + 1000;
		tv.setText(String.valueOf(value) + " m");
	}   
}
